unit MapProperties;

interface

uses
  Database,
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Menus, StdCtrls, Buttons, ColorGrd, ExtCtrls, Db, DBTables;

type
  TFormMapProperties = class(TForm)
    MemoMap: TMemo;
    BtnOK: TBitBtn;
    BtnCancel: TBitBtn;
    MainMenu: TMainMenu;
    MenuEsc: TMenuItem;
    ColorGrid1: TColorGrid;
    Label2: TLabel;
    ColorGrid2: TColorGrid;
    Label3: TLabel;
    Bevel1: TBevel;
    Example: TImage;
    Bevel2: TBevel;
    Bevel3: TBevel;
    Label4: TLabel;
    ColorGrid3: TColorGrid;
    Bevel4: TBevel;
    Label1: TLabel;
    Label5: TLabel;
    Bevel5: TBevel;
    ColorGrid4: TColorGrid;
    Label6: TLabel;
    EditAttic: TComboBox;
    Label7: TLabel;
    EditBasement: TComboBox;
    BtnClear: TBitBtn;
    BtnClear2: TBitBtn;
    CheckPrint: TCheckBox;
    Bevel6: TBevel;
    ColorGrid5: TColorGrid;
    procedure BtnCancelClick(Sender: TObject);
    procedure BtnOKClick(Sender: TObject);
    procedure ColorGrid1Change(Sender: TObject);
    procedure BtnClearClick(Sender: TObject);
  private
    { Private declarations }
    procedure UpdateExample;
  public
    { Public declarations }
    map: string;
    procedure Setup;
  end;

var
  FormMapProperties: TFormMapProperties;

implementation
uses
   DBClient;

{$R *.DFM}

procedure TFormMapProperties.Setup;
var table: TClientDataset;
begin
  table:=TClientDataset.create(self);
  with table do begin
    CloneCursor(Datawell.TableMaps, false);
    active:=true;
    first;
    while not EOF do begin
      if fieldbyname('Map').asstring <> map then
        EditBasement.items.add(fieldbyname('Map').asstring);
      next;
    end;
  end;
  table.free;
  EditAttic.items.assign(EditBasement.items);
end;

procedure TFormMapProperties.BtnCancelClick(Sender: TObject);
begin
  close;
end;

procedure TFormMapProperties.BtnOKClick(Sender: TObject);
var
  table: TClientDataset;
  didfind, setmap: boolean;
begin
  if (EditBasement.text = EditAttic.text) and (EditBasement.text <> '') then begin
    messagedlg('The attic and basement levels cannot be the same',mtinformation,[mbok],0);
    EditBasement.selectall;
    EditBasement.setfocus;
    exit;
  end;
  table:=TClientDataset.create(self);
  with table do begin
    CloneCursor(Datawell.TableMaps, false);
    active:=true;
    //Check basement of selected attic level
    didfind:=false;
    first;
    while not EOF do begin
      if fieldbyname('Map').asstring = EditAttic.text then begin
        didfind:=true;
        break;
      end;
      next;
    end;
    if didfind then begin
      setmap:=false;
      if fieldbyname('S2').asstring = '' then setmap:=true
      else begin
        if fieldbyname('S2').asstring <> map then begin
          if messagedlg('Map level '+EditAttic.text+'''s basement is currently set to: '+
            fieldbyname('S2').asstring+#13+'Do you wish to set it to this map? ('+map+')',
            mtconfirmation,[mbyes,mbno],0) = mryes then setmap:=true;
        end;
      end;
      if setmap then begin
        edit;
        fieldbyname('S2').asstring:=map;
        post;
      end;
    end;
    //Check attic of selected basement level
    didfind:=false;
    first;
    while not EOF do begin
      if fieldbyname('Map').asstring = EditBasement.text then begin
        didfind:=true;
        break;
      end;
      next;
    end;
    if didfind then begin
      setmap:=false;
      if fieldbyname('S3').asstring = '' then setmap:=true
      else begin
        if fieldbyname('S3').asstring <> map then begin
          if messagedlg('Map level '+EditBasement.text+'''s attic is currently set to: '+
            fieldbyname('S3').asstring+#13+'Do you wish to set it to this map? ('+map+')',
            mtconfirmation,[mbyes,mbno],0) = mryes then setmap:=true;
        end;
      end;
      if setmap then begin
        edit;
        fieldbyname('S3').asstring:=map;
        post;
      end;
    end;
  end;
  table.free;
  Datawell.TableMaps.edit;
  Datawell.TableMaps.fieldbyname('Memo').assign(MemoMap.lines);
  Datawell.TableMaps.fieldbyname('FColor').asinteger:=
    Datawell.FigureColor(ColorGrid1.foregroundindex);
  Datawell.TableMaps.fieldbyname('BColor').asinteger:=
    Datawell.FigureColor(ColorGrid1.backgroundindex);
  Datawell.TableMaps.fieldbyname('LColor').asinteger:=
    Datawell.FigureColor(ColorGrid2.foregroundindex);
  Datawell.TableMaps.fieldbyname('SColor').asinteger:=
    Datawell.FigureColor(ColorGrid3.foregroundindex);
  Datawell.TableMaps.fieldbyname('I3').asinteger:=
    Datawell.FigureColor(ColorGrid4.foregroundindex);
  Datawell.TableMaps.fieldbyname('I2').asinteger:=
    Datawell.FigureColor(ColorGrid4.backgroundindex);
  Datawell.TableMaps.fieldbyname('L1').asboolean:=CheckPrint.checked;
  Datawell.TableMaps.fieldbyname('I4').asinteger:=
    Datawell.FigureColor(ColorGrid5.foregroundindex);
  Datawell.TableMaps.fieldbyname('S2').asstring:=EditBasement.text;
  Datawell.TableMaps.fieldbyname('S3').asstring:=EditAttic.text;
  Datawell.TableMaps.post;
  close;
end;

procedure TFormMapProperties.ColorGrid1Change(Sender: TObject);
begin
  UpdateExample;
end;

procedure TFormMapProperties.BtnClearClick(Sender: TObject);
begin
  case TComponent(sender).tag of
   -1: EditAttic.text:='';
    1: EditBasement.text:='';
  end;
end;

procedure TFormMapProperties.UpdateExample;
var i, w, h, bo: integer;
begin
  w:=Example.width;
  h:=Example.height;
  bo:=8;
  with Example.canvas do begin
//Background color
    pen.color:=ColorGrid1.backgroundcolor;
    brush.color:=ColorGrid1.backgroundcolor;
    rectangle(0,0,w,h);
//Plot horizontal gridlines
    pen.width:=2;
    pen.color:=ColorGrid1.foregroundcolor;
    i:=15;
    while i < h do begin
      moveto(0,i); lineto(w,i);
      inc(i,25);
    end;
//Plot vertical gridlines
    i:=15;
    while i < w do begin
      moveto(i,0); lineto(i,h);
      inc(i,25);
    end;
//Plot basement links
    pen.color:=ColorGrid4.backgroundcolor;
    moveto(27+bo,38+bo); lineto(27+bo,67+bo);
//Plot basement rooms
    brush.color:=clwhite;
    rectangle(17+bo,17+bo,39+bo,39+bo);
    rectangle(17+bo,67+bo,39+bo,89+bo);
//Plot stubs
    pen.color:=ColorGrid3.foregroundcolor;
    moveto(27,5); lineto(27,27);
    moveto(50,5); lineto(27,27);
    moveto(55,77); lineto(77,77);
//Plot links
    pen.color:=clblack;
    moveto(27,27); lineto(77,77);
    moveto(77,77); lineto(135,77);
//Plot rooms
    brush.color:=clwhite;
    rectangle(17,17,39,39);
    rectangle(67,67,89,89);
    rectangle(117,67,135,89);
//Plot label
    brush.style:=bsclear;
    font.name:='Arial';
    font.size:=14;
    font.style:=[fsbold];
    font.color:=ColorGrid2.foregroundcolor;
    textout(68,93,'Label');
//Plot attic links
    pen.width:=1;
    pen.style:=psdot;
    pen.color:=ColorGrid4.foregroundcolor;
    moveto(88-bo,27-bo); lineto(116-bo,27-bo);
    moveto(77-bo,0-bo); lineto(77-bo,16-bo);
//Plot attic rooms
    rectangle(67-bo,17-bo,89-bo,39-bo);
    rectangle(117-bo,17-bo,139-bo,39-bo);
  end;
end;

end.
